package com.augurit.smartform.dao;

import java.util.List;
import java.util.Map;

import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import com.augurit.common.base.dao.BaseDao;
import com.augurit.smartform.converter.ComponentDefinitionConverter;
import com.augurit.smartform.entity.SmComponentDefinition;
import com.augurit.smartform.web.component.form.ComponentDefinitionForm;

/**
 * 智能表单组件DAO类
 */
@Repository
public class SmComponentDefinitionDao extends BaseDao<SmComponentDefinition,Long>{
	/**
	 * 根据组件目录ID查询组件
	 * @param registerId
	 * @return
	 */
	public List<SmComponentDefinition> getDefinitionByRegisterId(Long registerId){
		String hql="from SmComponentDefinition where registerId="+registerId;
		return this.find(hql, new Object[]{});
	}
	
	/**
	 * 根据TAG属性查询组件
	 * @param registerId
	 * @return
	 */
	public SmComponentDefinition getDefinitionByTag(String tag){
		String hql="from SmComponentDefinition where tag='"+tag+"'";
		return this.findUnique(hql, new Object[]{});
	}
	
	/**
	 * 禁用某个组件目录下面的所有组件
	 * @param registerId
	 */
	public void disableByRegisterId(Long registerId){
		String hql="update SmComponentDefinition set available='0' where registerId="+registerId;
		this.batchExecute(hql, new Object[]{});
	}
	
	/**
	 * 启用某个组件目录下面的所有组件
	 * @param registerId
	 */
	public void enableByRegisterId(Long registerId){
		String hql="update SmComponentDefinition set available='1' where registerId="+registerId;
		this.batchExecute(hql, new Object[]{});
	}
	
	/**
	 * 得到系统所有可用组件
	 * @return
	 */
	public List<Map> getAllAvailabeComponent(){
		String hql="select tag as name, viewContent as view, content as xml from SmComponentDefinition where available='1'";
		return this.getSession().createQuery(hql).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
	}
}
